void main_move() { PCZ_TITLE("std::move operation: threads"); std::function<void(int, int)> fun = &print_add; std::thread thread(fun, 1, 1); #if METHOD_NR == 1 join_thread(std::move(thread)); #elif METHOD_NR == 2 join_thread(thread); #elif METHOD_NR == 3 std::thread thread2 (std::move(thread)); // std::thread thread2 (thread); // error: use of deleted function ‘std::thread::thread(std::thread&)’ PCZ_DEBUG("thread.joinable() = %d", thread.joinable()); // -> 0 PCZ_DEBUG("thread2.joinable() = %d", thread2.joinable()); // -> 1 thread_joiner(std::move(thread2)); #endif PCZ_TITLE("std::move operation: vector"); // move a vector std::vector<int> v = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; PCZ_DEBUG("vector.size() = %d, pointer to data = %x", v.size(), v.data()); std::vector<int> v2 = std::move(v); PCZ_DEBUG("vector_2.size() = %d, vector.size() = %d, pointer_2 = %x, pointer_1 = %x", v2.size(), v.size(), v2.data(), v.data()); PCZ_TITLE("std::move operation: return by value"); simple_object s = return_simple_object(); // here s is not a new instance, but is the same as in the function PCZ_DEBUG("simple_object is moved to this variable - pointer = %x", &s); }
int main() { //initialize log timer struct sigaction action; struct itimerval timer; memset(&action, 0, sizeof(action)); action.sa_handler = &timer_handler; sigaction(SIGVTALRM, &action, NULL); timer.it_value.tv_sec = 0; timer.it_value.tv_usec = 100000; timer.it_interval.tv_sec = 0; timer.it_interval.tv_usec = 100000; setitimer(ITIMER_VIRTUAL, &timer, NULL); //initialize mutexes (is the plural mutecies?) pthread_mutex_init(&ThreadListMutex, NULL); pthread_mutex_init(&FilePrinting, NULL); pthread_mutex_init(&CountMutex,NULL); pthread_mutex_init(&dirCountMutex,NULL); //create an initialize html file htmlFile = fopen("./catalog.html","w"); fprintf(htmlFile, "<html>\n\t<head>\n\t\t<title>My Image Manager</title>\n\t</head>\n<body>\n"); fflush(htmlFile); //replace with directory to scan char *dir = "./dir"; printf("\n\n\n\nBeginning scan of directory: %s\n","/home/schod005/Documents/csci4061/A4/dir"); thread_creator(dir); //create thread for input directory thread_joiner(); fprintf(htmlFile, "\n</body>\n</html>\n"); fflush(htmlFile); fclose(htmlFile); printf("\nScan complete\n"); printf("Number of images: %d\n",count); return 0; }
int main(int argc, char *argv[]) { //get arguments if(getArgs(argc,argv) == 1) {return 1;} printf("\nBeginning program...\n"); //create output.log and return if error char output[strlen(output_dir) + 12]; strcpy(output, output_dir); strcat(output, "/output.log"); outputFile = fopen(output,"a"); if (outputFile == NULL) { printf("ERROR: could not open log file: %s\n", output); return 1; } fprintf(outputFile, "\n------------------------------------------------\n\n"); fprintf(outputFile, "Beginning program...\nVersion: %s\n\n", version); //initialize catalog.log file and timer char log[strlen(output_dir) + 13]; strcpy(log, output_dir); strcat(log, "/catalog.log"); LogFile = fopen(log,"a"); if (outputFile == NULL) { printf("ERROR: could not open log file: %s\n", log); return 1; } fprintf(LogFile, "\n------------------------------------------------\n\n"); fprintf(LogFile,"Log for variant %s:\n\n", version); struct sigaction action; struct itimerval timer; memset(&action, 0, sizeof(action)); action.sa_handler = &timer_handler; sigaction(SIGALRM, &action, NULL); //set up signal handler timer.it_value.tv_sec = 0; timer.it_value.tv_usec = 1000; //timer goes off every 1 ms timer.it_interval.tv_sec = 0; timer.it_interval.tv_usec = 1000; setitimer(ITIMER_REAL, &timer, NULL); //initialize mutexes pthread_mutex_init(&ThreadListMutex, NULL); pthread_mutex_init(&htmlMutex, NULL); pthread_mutex_init(&fileCountMutex,NULL); pthread_mutex_init(&dirCountMutex,NULL); //create and initialize html file char htmlFileName[strlen(output_dir) + 14]; strcpy(htmlFileName, output_dir); strcat(htmlFileName, "/catalog.html"); htmlFile = fopen(htmlFileName,"w"); fprintf(htmlFile, "<html>\n\t<head>\n\t\t<title>My Image Manager</title>\n\t</head>\n<body>\n"); fflush(htmlFile); //get starting times and create threads in input_directory fprintf(outputFile,"Beginning scan of directory: %s\n",input_dir); time_t startTime = time(NULL); struct timeval timerTime; gettimeofday(&timerTime, NULL); long int TotalTime = timerTime.tv_usec; thread_creator(input_dir); //create thread for input directory //join threads until search is done thread_joiner(); //get final times for total run time gettimeofday(&timerTime, NULL); TotalTime = (long int)timerTime.tv_usec - TotalTime; StopTime = 1; //stop timing threads //finish html file fprintf(htmlFile, "\n</body>\n</html>\n"); fflush(htmlFile); fclose(htmlFile); //finish updating output.log fprintf(outputFile,"\nScan complete\n"); fprintf(outputFile,"Number of images: %d\n",fileCount); fprintf(outputFile,"Number of directories: %d\n",dirCount); fprintf(outputFile,"Number of threads: %d\n",threadCount); //finish updating catalog.log file fprintf(LogFile,"\nProgram Initiation: %s\n", asctime(localtime(&startTime))); fprintf(LogFile,"Number of JPG files: %d\n", numJPG); fprintf(LogFile,"Number of BMP files: %d\n", numBMP); fprintf(LogFile,"Number of PNG files: %d\n", numPNG); fprintf(LogFile,"Number of GIF files: %d\n", numGIF); fprintf(LogFile,"\nTotal number of valid image files: %d\n", fileCount); fprintf(LogFile,"Total number of directories: %d\n", dirCount); fprintf(LogFile,"Total number of threads created: %d\n", threadCount); fprintf(LogFile,"\nTotal time of execution: %f ms\n", (double)TotalTime/1000.0); fprintf(LogFile,"\nEnd of log\n"); fflush(LogFile); fclose(LogFile); fprintf(outputFile, "Done!\n"); fflush(outputFile); fclose(outputFile); printf("Done!\n\n"); return 0; }