首页 > 其他分享 >Multithreaded programming

Multithreaded programming

时间:2024-10-20 20:24:08浏览次数:1  
标签:thread create programming will threads Multithreaded each array

Lab02: Multithreaded programming

Due date Please refer to the lab assignment requirements.

Goal The goal of this project is (1) to obtain a good understanding of multi-threading, (2) topractice creating threads and coordinate the running of the threads.

Part I—Sudoku Solution Validator

A Sudoku puzzle uses a 9 × 9 grid in which each column and row, as well as each of thenine 3 × 3 subgrids, must contain all of the digits 1 · · · 9. The following figure presents anexample of a valid Sudoku puzzle. This project consists of designing a multithreadedapplication that determines whether the solution to a Sudoku puzzle isvalid.There are several different ways of multithreading this application. One suggestedstrategy is to create threads that check the following criteria:

 A thread to check that each column contains the digits 1 through 9

 A thread to check that each row contains the digits 1 through 9

 Nine threads to check that each of the 3 × 3 subgrids contains the digits 1

through 9This would result in a total of eleven separate threads for validating a Sudoku puzzle.However, you are welcome to create even more threads for this project. For example,rather than creating one thread that checks all nine columns, you could create nineseparate threads and have each of them check one column.Passing Parameters to Each Thread The parent thread will create the worker threads, passing each worker the location that it

must check in the Sudoku grid. This step will require passing several parameters to eachthread. The easiest approach is to create a data structure using a struct . Forexample, astructure to pass the row and column where a thread must begin validating would appearas follows:/* structure for passing data to threads */typedef struct

{int row;int column;

} parameters;Both Pthreads and Windows programs will create worker threads using a strategy similar

to that shown below:parameters *data = (parameters *) 代 写Multithreaded programming malloc(sizeof(parameters));data->row = 1;data->column = 1;/* Now create the thread passing it data as a parameter */The data pointer will be passed to either the pthread_create() (Pthreads) function orthe CreateThread() (Windows) function, which in turn will pass it as a parameter tothe function that is to run as a separate thread.

Returning Results to the Parent Thread

Each worker thread is assigned the task of determining the validity of a particular region ofhe Sudoku puzzle. Once a worker has performed this check, it must pass its results backto the parent. One good way to handle this is to create an array of integer values that isvisible to each thread. The i th index in this array corresponds to the i th worker thread. If aworker sets its corresponding value to 1, it is indicating that its region of the Sudokupuzzle is valid. A value of 0 would indicate otherwise. When all worker threads havecompleted, the parent thread checks each entry in the result array to determine if theSudoku puzzle is valid.

Part II—Multithreaded Sorting Application

Write a multithreaded sorting program that works as follows: A list of integers is divided

nto two smaller lists of equal size. Two separate threads (which we will term sortingthreads) sort each sublist using a sorting algorithm of your choice. The two sublists arethen merged by a third thread—a merging thread —which merges the two sublists into asingle sorted list.Fig 2. Multithreaded sorting.Because global data are shared cross all threads, perhaps the easiest way to set up thedata is to create a global array. Each sorting thread will work on one half of this array. Asecond global array of the same size as the unsorted integer array will also be established.The merging thread will then merge the two sublists into this second array. Graphically,this program is structured according to Figure 2. This programming project will requirepassing parameters to each of the sorting threads. In particular, it will be necessary toidentify the starting index from which each thread is to begin sorting. Refer to theinstructions in Project 1 for details on passing parameters to a thread.The parent thread will output the sorted array once all sorting threads have exited.

Submission

Your submission should include the code, a readme file briefly describing your design,how to compile/use your code and, and a report which consists of (but not limited to) thefollowing parts:

 Summarize the thread control methods provided by Linux(or Windows, if applied),and describe the usage.

 Your design of the program

 Snapshots of experimental results(statistics) with analysis

 Problems encountered and your solution

 Reference materials

 Your suggestions and comments

Environment Linux (recommended, any kernel after 2.6 is fine) and C/C++.

ReferencesN/A

标签:thread,create,programming,will,threads,Multithreaded,each,array
From: https://www.cnblogs.com/goodlunn/p/18487006

相关文章

  • 2024 ICPC Asia Taiwan Online Programming Contest题解记录
    比赛链接:https://codeforces.com/gym/105383/problemA.AnimalFarm找个最大pig,然后所有比他小的其他种类生物一直加就好了#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;constllmod=1e9+7;llksm(llx,lly){ llans=1; while(y) { if(y&1)......
  • CS 551 Systems Programming
    CS551SystemsProgramming,Fall2024ProgrammingProject1Out:10/13/2024Sun.Due:10/26/2024Sat.23:59:59Inthisprojectyouraregoingtoimplementacustommemorymanagerthatmanagesheapmemoryallocationatprogramlevel.Herearethereasonswh......
  • Expression-bodied members (C# programming guide)
    Expressionbodydefinitionsletyouprovideamember'simplementationinaconcise,readableform.Youcanuseanexpressionbodydefinitionwheneverthelogicforanysupportedmember,suchasamethodorproperty,consistsofasingleexpression.A......
  • TECH.UB.25: Intro to Python Programming
    TECH.UB.25:IntrotoPythonProgramming:Assignment#4Scenario: CampusPizzaisreallytakingoffandyourco-founderslovetheprogramsyouhavebuilt. Theywantyoutobuildanobject-orientedprogramfortheirbeverages. Campuspizzahastwobeve......
  • C - Word Ladder (Toyota Programming Contest 2024#9 (AtCoder Beginner Contest 370)
    题目链接:C-WordLadder题目:样例:分析:不要被题目所吓到,一切长题目都是纸老虎。题目大意就是给你两个字符串s和t,一次只能更换一个字母,求s变到t更换的次数,并输出每次更换一个字母后的最小字典序字符串。题意好理解,可以直接暴力,大力出奇迹。但是有没有更好的方法呢?既然问了......
  • HIAST Collegiate Programming Contest 2024(非完全题解)
    C题HZY做的,等他补题解//#pragmaGCCoptimize("O3,unroll-loops")//#pragmaGCCtarget("avx2,bmi,bmi2,lzcnt,popcnt")////如果在不支持avx2的平台上将avx2换成avx或SSE之一#include<bits/stdc++.h>usingnamespacestd;#definexfirst#defineysecon......
  • 【ICPC】The 2021 ICPC Asia Shanghai Regional Programming Contest I
    SteadilyGrowingSteam#动态规划#背包#枚举题目描述AliceenjoysplayingacardgamecalledSteadilyGrowingSteam(asknownasSGS).Inthisgame,eachplayerwillplaydifferentrolesandhavedifferentskills.Playersgetcardsfromthedeckandu......
  • 【ICPC】The 2021 ICPC Asia Shanghai Regional Programming Contest G
    EdgeGroups#树形结构#组合数学#树形dp题目描述Givenanundirectedconnectedgraphofnnnverticesandn......
  • 【ICPC】The 2021 ICPC Asia Shanghai Regional Programming Contest H
    LifeisaGame#最小生成树#重构树#图论#贪心题目描述Agoodproblemshouldhaveaconcisestatement.Youaregivenanarrayaaaoflength......
  • RBE104TC C/C++ Programming Language
    RBE104TCC/C++ProgrammingLanguageAssignment1ContributiontotheOverallMarks30%IssueDateSubmissionDeadline13thOctober2024AssignmentOverview:ThisassignmentisgearedtowardsassessingfundamentalcodingconceptsinC/C++andinitiatingthep......