| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | threads, threads, more threads |
04 Sep 94, Craig Swanson writes to Jari Laaksonen:
> 2) Any variables or resources that may have to be accessed by more
> than one thread at a time MUST be protected by a semaphore
> or some other mechanism in order to avoid corrupting the data
> structures a thread context switch occurs such that one thread
> hasn't finished modifying a data structure and then the next
> thread starts modifying it while it is in an inconsistent state.
This is NOT correct. While in many cases it is true that you need to use
protection mechanisms to prevent race conditions, it is not ALWAYS true.
Many operations are atomic or will not produce a race condition even if it
is not atomic.
For example, I use a variation on this code all the time:
int thread_finished;
void function_in_one_thread(void *dummy)
{
/* Some initialization goes here */
do
{
/* Some processing goes here */
} while(!thread_finished);
/* Some exit processing goes here */
}
void function_in_other_thread(void)
{
thread_finished = 0;
_beginthread(function_in_one_thread, NULL 131072, NULL);
getch();
thread_finished = 1;
/* usually I put a wait for the thread to terminate here */
}
In this case, access to the thread_finished variable does NOT have to be
protected by a semaphore because both reading and writing integers are
atomic operations. Read-modify-write is NOT an atomic operation, but I'm
not doing
that here.
- Jon
--- GoldED/2 2.42.G0214
* Origin: The Wandering Programmer Comes Home (1:106/2000.25)SEEN-BY: 12/2442 54/54 620/243 624/50 632/348 640/820 690/660 711/409 410 413 SEEN-BY: 711/430 807 808 809 934 942 712/353 623 713/888 800/1 @PATH: 30883/25 106/2000 449 116 170/400 280/1 396/1 3615/50 @PATH: 229/2 12/2442 711/409 54/54 711/808 809 934 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.