Does C++ provide synchronize method?

Started by
5 comments, last by wah_on_2 22 years ago
Does C++ provide synchronized method like that: [Java] class Test{ synchronized void Something(){.....} } If yes, how to do that? Thanks
Advertisement
No, you need to manually use some synchronization method (events, mutexes, semaphores)

Forever trusting who we are
And nothing else matters
- Metallica
Forever trusting who we areAnd nothing else matters - Metallica
No, it doesn''t, and it doesn''t really need them, either, because it has no notion of threads.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
Which in turn means that if you find yourself some thread implementation, that will have its own facilities for enforcing synchronisation. (Which admittedly won''t be as clean as Java''s, but is potentially more flexible.)

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions ]
Mm.....I have two threads. Those are GameController and NetworkController. Also, i have a SharedMemory Handler.
The GameController handles the game flow. e.g. hero move in map...
NetworkController handles the send and receive data from/to server....
The SharedMemory Handler is a common area that store all other online player''s heros data those are in the same map...

I want to make one and only one controller can access the SharedMemory''s data at a time. So i remember java provided synchronized method for me to do that. How can i do that at C++?


Threads are not functions of C/C++--they're only available through libraries, usually interfacing the OS. How are you starting your thread? If you tell us what library you're using for threads, we can probably tell you what synchronization primitives the library offers.

For example, if you're using win32, you've got InitializeCriticalSection, CreateMutex, CreateSemaphore, etc.

[edited by - Stoffel on April 17, 2002 1:09:34 PM]
quote:Original post by Stoffel
Threads are not functions of C/C++--they're only available through libraries, usually interfacing the OS.


I read They plan to add them in they next version of the standard library though.

Edit: forgot the capital T

[edited by - Fruny on April 17, 2002 1:30:40 PM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement