comp.lang.c++@googlegroups.com | Google Groups | ![]() |
Unsure why you received this message? You previously subscribed to digests from this group, but we haven't been sending them for a while. We fixed that, but if you don't want to get these messages, send an email to comp.lang.c+++unsubscribe@googlegroups.com. |
- Zombie threads on AIX... - 1 Update
- sleep and sleep_for on Linux - 9 Updates
- The importance of sausages... - 1 Update
- An example from an online test.... - 7 Updates
- Onwards and upwards - 1 Update
redboy1972 <redboy1972@live.com>: Oct 10 03:43PM -0500 I am having issues on AIX 6.1 where once a thread is created it appears not to die. Compile with: gcc -pthread sample.c cc_r sample.c Both have same result. I have attempted to detach the thread in three ways. 1) Main method calls pthread_detach after its created. 2) Main method attempts to create thread detached through attr arg. 3) Thread function calling pthread_detach. My attempts to track thread start/stop result in all threads finishing. ALl three as per this call have numerous threads in state Z -bash-4.2# ps -mo THREAD -p `ps -ef | grep a.out | awk '{print $2}'` USER PID PPID TID ST CP PRI SC WCHAN F TT BND COMMAND root 340028 278750 - A 0 60 11 f100010019c574b0 200001 pts/0 - ./a.out 10 3 - - - 450679 Z 0 60 1 - c00001 - - - - - - 561215 Z 0 60 1 - c00001 - - - - - - 573523 Z 0 60 1 - c00001 - - - - - - 663653 Z 0 60 1 - c00001 - - - - - - 684203 Z 0 60 1 - c00001 - - - - - - 745711 Z 0 60 1 - c00001 - - - - - - 753695 Z 0 60 1 - c00001 - - - - - - 987139 Z 0 60 1 - c00001 - - - - - - 1007867 Z 0 60 1 - c00001 - - - - - - 1523761 S 0 60 1 f100010019c574b0 410400 - - - - - - 1536067 Z 0 60 1 - c00001 - - - Here is the code (I know its sloppy! Please excuse!) #include <stdlib.h> #include <stdio.h> #include <string.h> #include <ctype.h> #include <unistd.h> #include <pthread.h> #include <limits.h> #define DETACH_NONE 0 #define DETACH_METHOD_DETACH_IN_MAIN 1 #define DETACH_AT_PTHREAD_CREATE 2 #define DETACH_IN_THREAD_FUNC 3 int g_nDetachMethod = DETACH_METHOD_DETACH_IN_MAIN; void increment_count(); void decrement_count(); long long get_count(); void set_count( long long c ); void *PrintHello2( void* ptr ); void displayThreadStatus( pthread_t tid ); void displayTopOutput(); void *PrintHello2( void* ptr ) { int rc; pthread_t tid = pthread_self(); sched_yield(); increment_count(); sleep( rand() % 10 + 1 ); displayThreadStatus( tid ); if( g_nDetachMethod == DETACH_IN_THREAD_FUNC ) { rc = pthread_detach( tid ); printf("\t\tDetach result: %d.\n", rc); displayThreadStatus( tid ); } decrement_count(); pthread_exit (NULL); } int main( int argc, char *argv[] ) { pthread_t thds; int i, count, num_threads, rc; set_count( 0 ); if( argc < 2 ) { printf("Specify on the command line which detach method to use and the number of threads.\n"); printf("Usage: %s threads detachmethod\n", argv[0]); printf("\t threads: The count of child threads to spawn. Default=10\n"); printf("\tdetachmethod: The point at which detach should be called. Default=%d\n", DETACH_METHOD_DETACH_IN_MAIN); printf("\t\t\t%d: Do not attempt to detach. Default behaviour is joinable.\n"); printf("\t\t\t%d: Call pthread_detach right after pthread_create in main.\n", DETACH_METHOD_DETACH_IN_MAIN); printf("\t\t\t%d: Call pthread_create with pthread_attr_setdetachstate attribute set to PTHREAD_CREATE_DETACHED.\n", DETACH_AT_PTHREAD_CREATE); printf("\t\t\t%d: Call pthread_detach inside the thread function.\n", DETACH_IN_THREAD_FUNC); printf("\nExample:\n"); printf("\t a.out 10 %d\n", DETACH_METHOD_DETACH_IN_MAIN); exit(0); } printf("Using Threads:\"%s\" Detach method:\"%s\"\n", argv[1], argv[2]); num_threads = atoi( argv[1] ); g_nDetachMethod = atoi( argv[2] ); printf("\n\n======= CREATING %d THREADS =======\n", num_threads); for( i = 0; i < num_threads; i++ ) { printf( "\n\tcreating thread : %d \n", i ); if( g_nDetachMethod == DETACH_AT_PTHREAD_CREATE ) { pthread_attr_t tattr; pthread_attr_init(&tattr); pthread_attr_setdetachstate(&tattr,PTHREAD_CREATE_DETACHED); pthread_create( &thds, &tattr, PrintHello2, (void*) NULL ); } else { pthread_create( &thds, NULL, PrintHello2, (void*) NULL ); if( g_nDetachMethod == DETACH_METHOD_DETACH_IN_MAIN ) { rc = pthread_detach( thds ); printf("\t\tDetach result: %d.\n", rc); } } } printf("\n\n======= WAITING FOR THREADS TO FINISH =======\n"); for( i = 0; i < 20; i++ ) { sleep( 1 ); count = get_count(); printf( "\tLoop: %d, Active Threads: %d, Sleep seconds: %d\n", i, count, (i + 1) ); if( count == 0 ) break; } displayTopOutput(); printf("\n\n======= WAITING FOR 10 SECONDS TO MANUALLY QUERY FOR DEFUNCT PROCESSES =======\n"); printf("ps -mo THREAD -p `ps -ef | grep a.out | awk '{print $2}'`\n"); for( i = 10; i >= 0; i-- ) { sleep( 1 ); printf( "\r%d seconds remaining.", i ); fflush (stdout); } count = get_count(); printf("\n\n======= FINAL STATUS Active Threads: %d =======\n", count ); displayTopOutput(); return 0; } void displayThreadStatus( pthread_t tid ) { /* int state; pthread_attr_t attr; pthread_getattr_np( pthread_self(), &attr); pthread_attr_getdetachstate( &attr, &state ); printf("\t\t[%d]: %s\n", tid, state == PTHREAD_CREATE_DETACHED? "PTHREAD_CREATE_DETACHED": "PTHREAD_CREATE_JOINABLE"); */ } void displayTopOutput() { if( fork() != 0 ) { int status; wait( &status ); return; } pid_t pid = getppid(); char buffer[10]; sprintf( buffer, "%d", (int) pid ); printf( "PID: %s\n", buffer ); /* execl( "/usr/bin/top", "top", "-H", "-b", "-n", "1", "-d", "1", "-p", buffer, (char *) NULL ); */ execl("/usr/bin/ps", "ps", "-mo", "THREAD", "-p", buffer, (char *) NULL ); printf("\n"); exit( 0 ); } pthread_mutex_t count_mutex; long long count=0; void increment_count() { pthread_mutex_lock( &count_mutex ); count = count + 1; pthread_mutex_unlock( &count_mutex ); } void decrement_count() { pthread_mutex_lock( &count_mutex ); count = count - 1; pthread_mutex_unlock( &count_mutex ); } long long get_count() { long long c; pthread_mutex_lock( &count_mutex ); c = count; pthread_mutex_unlock( &count_mutex ); return (c); } void set_count( long long c ) { pthread_mutex_lock( &count_mutex ); count = c; pthread_mutex_unlock( &count_mutex ); } |
Emanuel Berg <embe8573@student.uu.se>: Oct 10 03:29AM +0200 I would like to have the program sleep for a fixed amount of milliseconds each time a loop iterates. I tried two approaches, starting from the int variable "rate" - how many milliseconds the computer should sleep. Both seem to work only sleep seems to sleep much *less* than sleep_for. With sleep I have 791710 iterations of the loop, and with sleep_for - 2975! Can anyone explain this? What is the correct way to sleep for x milliseconds? sleep(rate/1000); std::this_thread::sleep_for(std::chrono::milliseconds(rate)); Help very much appreciated. -- underground experts united |
"Jouko Koski" <joukokoskispam101@netti.fi>: Oct 10 08:49AM +0300 > "rate" - how many milliseconds the computer should > sleep. > sleep(rate/1000); Considering declaration unsigned int sleep(unsigned int seconds); and integer arithmetics, what do you expect rate/1000 to be? Isn't it obvious that > std::this_thread::sleep_for(std::chrono::milliseconds(rate)); is the preferred way? -- Jouko |
Geoff <geoff@invalid.invalid>: Oct 10 06:39AM -0700 On Fri, 10 Oct 2014 03:29:51 +0200, Emanuel Berg >I tried two approaches, starting from the int variable >"rate" - how many milliseconds the computer should >sleep. Time in milliseconds isn't a rate, it's a duration. >Both seem to work only sleep seems to sleep much >*less* than sleep_for. The sleep function has units of seconds, why do you think your "rate" in milliseconds divided by 1000 would change that? The smallest quantum of sleep() you can get is 1 second. >With sleep I have 791710 iterations of the loop, and >with sleep_for - 2975! >Can anyone explain this? man 3 sleep >What is the correct way to sleep for x milliseconds? std::this_thread::sleep_for(std::chrono::milliseconds(duration)); |
Emanuel Berg <embe8573@student.uu.se>: Oct 10 09:01PM +0200 > Considering declaration unsigned int sleep(unsigned > int seconds); and integer arithmetics, what do you > expect rate/1000 to be? Aha, I thought it was the same interface as /bin/sleep. -- underground experts united |
Emanuel Berg <embe8573@student.uu.se>: Oct 10 09:06PM +0200 > Time in milliseconds isn't a rate, it's a duration. Here it is a name of a variable that is read from the argument the user provides to express the rate "once, for every...". -- underground experts united |
Ben Bacarisse <ben.usenet@bsb.me.uk>: Oct 10 08:22PM +0100 >> int seconds); and integer arithmetics, what do you >> expect rate/1000 to be? > Aha, I thought it was the same interface as /bin/sleep. That would not make any difference. You referred to the 'int variable "rate"' so rate/1000 is an integer no matter what that prototype for the sleep function is. -- Ben. |
Emanuel Berg <embe8573@student.uu.se>: Oct 10 09:24PM +0200 > the 'int variable "rate"' so rate/1000 is an integer > no matter what that prototype for the sleep function > is. Yes, I understood, I actually tried stuff like 0.001 as well but got the same (lack of) result. -- underground experts united |
Ike Naar <ike@iceland.freeshell.org>: Oct 10 07:33PM > I would like to have the program sleep for a fixed > amount of milliseconds each time a loop iterates. Don't sleep till some time has elapsed. Do sleep till an intersting event has happened. |
Emanuel Berg <embe8573@student.uu.se>: Oct 10 10:19PM +0200 > Don't sleep till some time has elapsed. Do sleep > till an intersting event has happened. Indeed, feel free to help me with that as well! The purpose is to have the program terminate after a certain number of LLC misses. Usage: llc ALLOWED-LLC-MISSES PID POLL-RATE-MS Here is the poll loop, starting at line 57 in llc.cc: for (long long polls = 0, count = 0, old_count = -1; count < max_misses; polls++) { read(fd, &count, sizeof(count)); if (count != old_count) { std::cout << "LLC misses after " << polls << " polls: " << count << std::endl; old_count = count; } std::this_thread::sleep_for(std::chrono::milliseconds(rate)); } Here is the program: http://user.it.uu.se/~embe8573/llc/ -- underground experts united |
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Oct 10 06:26PM +0100 ... cannot be overestimated. My C++ XML library (free and open source): http://i42.co.uk/stuff/NoFussXML.htm Probably the fastest on the planet (well it's faster than RapidXML as used in Boost). /Flibble |
Robert Wessel <robertwessel2@yahoo.com>: Oct 09 09:57PM -0500 On Thu, 9 Oct 2014 12:10:22 -0700 (PDT), "Rick C. Hodgin" >which existed in the signed value. It just now has no respect of sign >in operations. For positive signed int values, it's left exactly as it >is. For negative signed int values it will now be a very large value. That would certainly not be true on a one's-complement or signed magnitude implementation. For example converting a -5 in a 16-bit int from signed to unsigned will result in the value 65531 in any implementation. That works fine on a two's complement implementation. On a one's complement machine it would be converting from a bit pattern of 0xfffa to 0xfffb, on a sign/mag machine, you'd convert from 0x8005 to 0xfffb. After bit reversing (in all cases), you'd have 0xdfff, or -8193, which you'll then convert back to signed. Again, OK on the two's complement machine. On the ones complement machine you'd end up with 0xdffe, and, I think, on a sign/mag machine, 0xa001. And neither of those really match what I'd expect a bit reversal of the original values would be. And that's assuming you don't hit a trap representation for the one's complement or sign/mag implementations. |
David Harmon <source@netcom.com>: Oct 10 08:28AM -0700 On Thu, 09 Oct 2014 11:26:16 -0500 in comp.lang.c++, Robert Hutchings <rm.hutchings@gmail.com> wrote, >Given an variable int N, write a function that will reverse all the bits >and return that changed integer variable. Since this is a c++ quiz, perhaps you should add "by using std::reverse()". |
Robert Hutchings <rm.hutchings@gmail.com>: Oct 10 10:49AM -0500 On 10/10/2014 10:28 AM, David Harmon wrote: >> and return that changed integer variable. > Since this is a c++ quiz, perhaps you should add > "by using std::reverse()". Looking at the variety of answers both here and on the web, I think this question is more about creative thinking skills. There are many ways to skin a cat, but which way is best? |
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Oct 10 05:56PM +0100 On 10/10/2014 16:49, Robert Hutchings wrote: > Looking at the variety of answers both here and on the web, I think this > question is more about creative thinking skills. There are many ways to > skin a cat, but which way is best? Skinning a cat isn't a very Christian thing to say mate; re-educate yourself to say "crack a nut" in future. Oh sorry, I forgot, the Christian god is actually quite evil .. carry on. /Flibble |
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Oct 10 05:57PM +0100 On 10/10/2014 17:56, Mr Flibble wrote: > Skinning a cat isn't a very Christian thing to say mate; re-educate > yourself to say "crack a nut" in future. Oh sorry, I forgot, the > Christian god is actually quite evil .. carry on. Oops, sorry .. I confused you with Rick C. Hodgin. Safely ignore. :) /Flibble |
Robert Hutchings <rm.hutchings@gmail.com>: Oct 10 12:06PM -0500 On 10/10/2014 11:56 AM, Mr Flibble wrote: > yourself to say "crack a nut" in future. Oh sorry, I forgot, the > Christian god is actually quite evil .. carry on. > /Flibble You know something mate? Your contributions to this group are very uh, shall we say, "poor". Sorry, but someone had to say it...and no, I have no sausages... |
Robert Hutchings <rm.hutchings@gmail.com>: Oct 10 12:08PM -0500 On 10/10/2014 12:06 PM, Robert Hutchings wrote: > You know something mate? Your contributions to this group are very uh, > shall we say, "poor". Sorry, but someone had to say it...and no, I have > no sausages... Whoops, my bad. Sorry, wrong person. Carry on! |
woodbrian77@gmail.com: Oct 09 06:22PM -0700 On Wednesday, October 8, 2014 12:13:09 AM UTC-5, Charles J. Daniels wrote: > Generic lambdas, part of C++14 which I've not worked with, would allow use of auto over the specific type, if I understand correctly. That would mirror your preferred implementation. There are already existing compilers that can handle such as a thing is also my understanding, including MSVC. Normally I'd have access to some compilers that have support for that but I switched from Linux to BSD recently and have clang 3.3. Anyway, good to know about that. I'm not using anything from the algorithm header currently so would have to add that to get none_of. On a Linux machine I used the -E option with g++ and found that including algorithm results in 46,288 lines. That's a lot of overhead for such a small function. Brian Ebenezer Enterprises - In G-d we trust. http://webEbenezer.net |
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page. To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com. |
No comments:
Post a Comment