<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=us-ascii"><meta name=Generator content="Microsoft Word 14 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
        {mso-style-priority:34;
        margin-top:0in;
        margin-right:0in;
        margin-bottom:0in;
        margin-left:.5in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
span.EmailStyle17
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-US link=blue vlink=purple><div class=WordSection1><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>WSAStartup() and WSACleanup() are safe to call multiple times (they are the Windows APIs I mentioned earlier). timeBeginPeriod() can also be called multiple times as long as it’s matched with the same number of timeEndPeriod()’s, so it appears everything is good.<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>I guess I should’ve just looked at the source after all, huh :)<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>> As for a definite answer, I'd just wrap ENet's initialize and use your own guard<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>That won’t be possible if I’m not directly responsible for enet, i.e. if I use a 3rd party library that internally depends on enet.<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>Thanks Ruud,<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'>Soren<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D'><o:p> </o:p></span></p><div style='border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt'><div><div style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in'><p class=MsoNormal><b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>From:</span></b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'> enet-discuss-bounces@cubik.org [mailto:enet-discuss-bounces@cubik.org] <b>On Behalf Of </b>Ruud van Gaal<br><b>Sent:</b> Wednesday, July 27, 2011 4:35 AM<br><b>To:</b> Discussion of the ENet library<br><b>Subject:</b> Re: [ENet-discuss] Calling enet_initialise() more than once<o:p></o:p></span></p></div></div><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal style='margin-bottom:12.0pt'>Ross incorrectly assumed you were talking about multithreading.<br>If you look at the source, win32.c, you see:<br><br>---<br>int enet_initialize (void)<br>{<br>    WORD versionRequested = MAKEWORD (1, 1);<br>    WSADATA wsaData;<br>   <br>    if (WSAStartup (versionRequested, & wsaData))<br>       return -1;<br><br>    if (LOBYTE (wsaData.wVersion) != 1||<br>        HIBYTE (wsaData.wVersion) != 1)<br>    {<br>       WSACleanup ();<br>       <br>       return -1;<br>    }<br><br>    timeBeginPeriod (1);<br><br>    return 0;<br>}<br><br>void<br>enet_deinitialize (void)<br>{<br>    timeEndPeriod (1);<br><br>    WSACleanup ();<br>}<br>---<br><br>I'm not sure what the behavior of WSAStartup and WSACleanup() is with respect to reference counting, but googling that will give you the answer.<br>I know that timeBeginPeriod() is really reference counted, but I think these days the timer is always at 1 ms resolution, so it doesn't really harm no matter how many calls you make.<br><br>As for a definite answer, I'd just wrap ENet's initialize and use your own guard:<br><br>static bool setup;<br>void InitEnet()<br>{<br>  if(!setup)<br>  {<br>    enet_initialize();<br>    setup=true;<br>  }<br>}<br>void CloseENet()<br>{<br>  if(setup)<br>  { enet_deinitialize();<br>   setup=false;<br>  }<br>}<br><br>That will always be safe (just the ugly global var which you might tuck away) and safeguard you from future changes in ENet wrt to the reference counted behavior.<br><br>Cheers,<br>Ruud<o:p></o:p></p><div><p class=MsoNormal>On Wed, Jul 27, 2011 at 7:12 AM, Soren Dreijer <<a href="mailto:dreijer@echobit.net">dreijer@echobit.net</a>> wrote:<o:p></o:p></p><p class=MsoNormal>I don't see how that answers the question. As the FAQ states, enet doesn't<br>use any significant global variables, but it definitely uses 'some' or else<br>we wouldn't be required to call a global initialize function. The question<br>is whether a second call will reset anything that existing ENetHost<br>structures rely on.<o:p></o:p></p><div><p class=MsoNormal style='margin-bottom:12.0pt'><br>> So my assumption would be NO, but you could try .. but why you would want<br>> to call this multiple times is a mystery, maybe rethink your application<br>> design ..<o:p></o:p></p></div><p class=MsoNormal>As I said in my original post, most APIs on Windows let you call initialize<br>functions multiple times in case you want to be absolutely sure something is<br>initialized before using it. What if you happen to use two (unrelated)<br>libraries in your project that both use enet internally? Unless those two<br>libraries defer to the caller to initialize enet (which might not be<br>intended at all if the library is supposed to hide away such details) then<br>there's no way for me to guarantee that enet_initialize() won't be called<br>twice in the same process.<br><br>I already worked around this 'issue' in my own code, but I'm mostly just<br>being proactive here and trying to determine if it'd be possible or not.<br><br>> -----Original Message-----<br>> From: <a href="mailto:enet-discuss-bounces@cubik.org">enet-discuss-bounces@cubik.org</a> [mailto:<a href="mailto:enet-discuss-">enet-discuss-</a><br>> <a href="mailto:bounces@cubik.org">bounces@cubik.org</a>] On Behalf Of Ross Linder<br>> Sent: Tuesday, July 26, 2011 11:39 PM<br>> To: Discussion of the ENet library<br>> Subject: Re: [ENet-discuss] Calling enet_initialise() more than once<o:p></o:p></p><div><div><p class=MsoNormal>><br>> I think the FAQ answers this pretty much ..<br>><br>> --><br>> Is ENet thread safe?<br>><br>> ENet does not use any significant global variables, the vast majority of<br>state<br>> is encapsulated in the ENetHost structure. As such, as long as the<br>> application guards access to this structure, then ENet should operate fine<br>in<br>> a multithreaded environment.<br>> <--<br>><br>> So my assumption would be NO, but you could try .. but why you would want<br>> to<br>> call this multiple times is a mystery, maybe rethink your application<br>> design ..<br>> --<br>><br>> On Tuesday 26 July 2011 20:09:09 Soren Dreijer wrote:<br>> > Am I allowed to call enet_initialize() more than once in my application?<br>> > I'm coming from the world of Windows where most APIs, such as<br>> WSAStartup()<br>> > or CoInitializeEx(), can be called multiple times as long as their<br>cleanup<br>> > counterparts are called the same number of times.<br>> ><br>> ><br>> ><br>> > Is that true for enet as well or will enet_initialize() reset any<br>already<br>> > initialized global state thereby potentially messing up other parts of<br>my<br>> > application that currently use enet?<br>><br>><br>> _______________________________________________<br>> ENet-discuss mailing list<br>> <a href="mailto:ENet-discuss@cubik.org">ENet-discuss@cubik.org</a><br>> <a href="http://lists.cubik.org/mailman/listinfo/enet-discuss" target="_blank">http://lists.cubik.org/mailman/listinfo/enet-discuss</a><br><br>_______________________________________________<br>ENet-discuss mailing list<br><a href="mailto:ENet-discuss@cubik.org">ENet-discuss@cubik.org</a><br><a href="http://lists.cubik.org/mailman/listinfo/enet-discuss" target="_blank">http://lists.cubik.org/mailman/listinfo/enet-discuss</a><o:p></o:p></p></div></div></div><p class=MsoNormal><o:p> </o:p></p></div></div></body></html>