User Tools

Site Tools


os_cp:threads

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
os_cp:threads [2024/04/17 23:28]
jkonczak
os_cp:threads [2024/04/18 11:42] (current)
jkonczak [Accessing the same data from multiple threads]
Line 330: Line 330:
 char text[1020]; char text[1020];
  
-void *updater(void *) {+void *updater(void * arg) {
   while (1)   while (1)
     for (char letter = '​a';​ letter <= '​z';​ ++letter) {     for (char letter = '​a';​ letter <= '​z';​ ++letter) {
Line 343: Line 343:
   pthread_t tid;   pthread_t tid;
   pthread_create(&​tid,​ NULL, updater, NULL);   pthread_create(&​tid,​ NULL, updater, NULL);
-  while (getchar(), 1)+  while (getchar() ​!= EOF)
     printf("​version:​ %llu\n ​  text: %s\n", version, text);     printf("​version:​ %llu\n ​  text: %s\n", version, text);
   return 0;   return 0;
Line 358: Line 358:
 unsigned long long counter; unsigned long long counter;
  
-void *incrementer(void *) {+void *incrementer(void * arg) {
   for (int i = 0; i < 1000; ++i)   for (int i = 0; i < 1000; ++i)
     counter++;     counter++;
Line 374: Line 374:
 } }
 </​code>​ </​code>​
 +<​html></​div></​html>​
 +<​html><​div style="​margin-top:​-1.4em;​ line-height:​ 1em"></​html>​
 +<​small>​If the code above always returns the right answer, try to run it a million times: \\
 +''​for X in `seq 1000000`; do RES=$(%%./​%%//​progname//​);​ test "​$RES"​ -ne 16000 && echo -e "​\n$RES"​ && break || echo -n '​.';​ done''​
 +</​small>​
 <​html></​div></​html>​ <​html></​div></​html>​
  
os_cp/threads.1713389299.txt.gz · Last modified: 2024/04/17 23:28 by jkonczak